package com.example.excel.ui.activity.login;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.example.excel.base.BaseViewModel;
import com.example.excel.base.ICallback;
import com.example.excel.config.Constants;
import com.example.excel.http.HttpManager;
import com.example.excel.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.Map;

public class LoginViewModel extends BaseViewModel {

    public MutableLiveData<Boolean> loginSuccess = new MutableLiveData<>();

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    public void login(final String userName, final String password) {
        Map<String, String> map = new HashMap<>();
        map.put("username", userName);
        map.put("password", password);
        HttpManager.getInstance().getApi().login(map).enqueue(new ICallback<Object>(this) {
            @Override
            public void onSuccess(Object o) {
                SharedPreferencesUtils.setString(Constants.SP_USER_NAME, userName);
                SharedPreferencesUtils.setString(Constants.SP_USER_PASS, password);
                loginSuccess.setValue(true);
            }
        });
    }
}
